<template>
    <view class="pub_page pub_page_bg pub_realname pub_header_padding">
        <pub-head :headerConfig="headerConfig"></pub-head>
        <!-- v-if="userInfo" -->
        <div class="pub_page_scroll page_box">
            <div class="pub_inputs" v-if="loadFinish">
                <div class="abox">
                    <div class="ali">
                        <div class="atitle">手机号</div>
                        <input class="ainput" type="text" placeholder="请输入手机号" :value="userInfo.mobile"
                            :disabled="true">
                    </div>
                    <div class="ali">
                        <div class="atitle">姓名</div>
                        <input class="ainput" type="text" placeholder="请输入姓名" v-model="name"
                            :disabled="isDisabled ? true : false">
                    </div>
                    <div class="ali">
                        <div class="atitle">身份证号</div>
                        <input class="ainput" type="text" placeholder="请输入身份证号" v-model="cardId"
                            :disabled="isDisabled ? true : false">
                    </div>
                    <!-- <div class="ali">
                        <div class="atitle">微信收款码</div>
                        <div class="img_lis">
                            <div class="img_li">
                                <pub-image class="img" src="../../static/aimg/img_3.jpg" />
                            </div>
                            <div class="img_no icon-font-basic icon-b-as-1"></div>
                        </div>
                    </div> -->
                </div>
                <div v-if="!isDisabled">
                    <br>
                    <div class="pub_btn" @click="toSave(1)">确定</div>
                </div>
            </div>
        </div>
    </view>
</template>

<script>
export default {
    data() {
        return {
            headerConfig: {
                title: '实名认证',//标题栏内容
                left_type: 3,
                header_color: ''
            },

            cardId: "",
            name: "",
            loadFinish: false
        };
    },
    computed: {
        isDisabled() {
            return this.userInfo?.cardId
        }
    },
    onLoad() {
        this.getUserInfo({
            callback: (info) => {
                this.cardId = info.cardId || '';
                this.name = info.cardName || '';
                this.loadFinish = true
            }
        })
    },
    methods: {
        async toSave(type) {
            if (type == 1) {
                if (!this.cardId || !this.name) {
                    this.$comFun.toast('请将身份信息填写完整');
                    return;
                }
                this.$comFun.openLoading();
                const res = await this.$request.post('user/verify', {
                    cardId: this.cardId,
                    name: this.name,
                });
                // console.log(res);
                this.$comFun.closeLoading();
                if (res.code == 200) {
                    this.$comFun.toast('认证成功');
                    this.reloadUserInfo({
                        cardId: this.cardId,
                        cardName: this.name,
                    });
                } else {
                    this.$comFun.toast(res.message);
                }
            } else if (type == 2) {
                if (!this.cardId || !this.name) {
                    this.$comFun.toast('请将身份信息填写完整');
                    return;
                }
                const APFaceDetectModule = uni.requireNativePlugin('AP-FaceDetectModule');
                // console.log(APFaceDetectModule);
                let metaInfo = APFaceDetectModule.getMetaInfo();
                if (this.$store.state.deviceType == 'ios') {
                    metaInfo = JSON.stringify(metaInfo);
                }
                // console.log(metaInfo);
                // console.log({
                //     cardId: this.cardId,
                //     name: this.name,
                //     metaInfo: metaInfo
                // });
                this.$comFun.openLoading();
                const res = await this.$request.post('user/getFaceVerifyCertifyId', {
                    cardId: this.cardId,
                    name: this.name,
                    metaInfo: metaInfo
                });
                // console.log(res);
                if (res.code == 200) {
                    const that = this;
                    const AliCertifyId = res.result;
                    APFaceDetectModule.verify({
                        certifyId: AliCertifyId,
                        ext_params_key_use_video: false,
                        returnVideo: false,
                        ext_params_key_ocr_bottom_button_clicked_color: '#037759',
                        ext_params_key_ocr_face_circle_color: '#037759',
                    }, function (bres) {
                        // console.log(bres);
                        if (bres.code == 1000 || bres.retCode == 1000) {
                            setTimeout(async () => {
                                const res_real = await that.$request.get('user/getFaceVerify', {
                                    certifyId: AliCertifyId
                                });
                                // console.log(res_real);
                                that.$comFun.closeLoading();
                                if (res_real.code == 200) {
                                    that.getInfo();
                                    that.$comFun.toast('认证成功');
                                } else {
                                    that.$comFun.toast(res_real.message);
                                }
                            }, 1000)
                        } else {
                            that.$comFun.closeLoading();
                            that.$comFun.toast('认证失败，请重试');
                        }
                    })
                } else {
                    this.$comFun.closeLoading();
                    this.$comFun.toast(res.message);
                }
            } else if (type == 3) {
                if (!this.cardId || !this.name) {
                    this.$comFun.toast('请将身份信息填写完整');
                    return;
                }

                this.$comFun.openLoading();
                const res = await this.$request.post('user/verify', {
                    cardId: this.cardId,
                    name: this.name,
                    metaInfo: '1'
                });
                // console.log(res);
                this.$comFun.closeLoading();
                if (res.code == 200) {
                    this.getInfo();
                    this.$comFun.toast('认证成功');
                } else {
                    this.$comFun.toast(res.message);
                }
            } else if (type == 4) {
                this.$comFun.toast('敬请期待');
            }
        }
    },
};
</script>

<style lang="scss" scoped>
@import "@/common/css/mine.scss";
</style>
